/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.geom.Dimension;

import com.bourre.medias.video.VideoDisplay;

import fever.display.ModalScreen;
import fever.Fever;
import fever.utils.Stringifier;

import fvaswing.components.reader.video.FvVideoItem;

/**
 * FullScreen video player mode for {@link fvaswing.components.reader.FvVideoPlayer} 
 * reader.
 * 
 * <p>Don't forget to add :
 * {@code
 *   mySWFObject.addParam( "allowFullScreen", "true" );
 * }
 * in your html document to allow fullscreen mode.
 * 
 * @author Romain Ecarnot
 */
class fvaswing.components.reader.video.FvFullScreenPlayer
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _screen : ModalScreen;
	private var _target : MovieClip;
	private var _videoMC : MovieClip;
	private var _controller : VideoDisplay;
	private var _isRunning : Boolean;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function FvFullScreenPlayer()
	{
		_isRunning = false;
	}	
	
	/**
	 * Starts fullscreen video session.
	 */
	public function open( item : FvVideoItem, time : Number ) : Void
	{
		if( !_isRunning )
		{
			_isRunning = true;
			_createTarget( item );
			
			Fever.application.setFullScreenMode( true );
			
			_controller.play( time );
		}
	}
	
	/**
	 * Closes player.
	 */
	public function close() : Void
	{
		if( _isRunning )
		{
			_isRunning = false;
			_target.removeMovieClip();
			
			_screen.dispose();
			delete _screen;
			
			_controller.release();
			delete _controller;	
		}
	}
	
	/**
	 * Returns {@code true} if player is running.
	 */
	public function isRunning() : Boolean
	{
		return _isRunning;	
	}
	
	/**
	 * Returns current playhead time.
	 */
	public function getPlayHeadTime() : Number
	{
		return _controller.getPlayheadTime();	
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _createTarget( item : FvVideoItem ) : Void
	{
		_screen = new ModalScreen( 0x000000, 100 );
		_target = _screen.getModalTarget();
		_videoMC = _target.attachMovie( 'VideoDisplay', '__full', 1 );
		
		_controller = new VideoDisplay( _videoMC, _videoMC['video'] );
		_controller.setAutoPlay( true );
		_controller.setAutoSize( false );
		_controller.setURL( item.getURL() );
		_layout( item );
		_screen.show();
	}
	
	private function _layout( item : FvVideoItem ) : Void
	{
		var videoSize : Dimension = item.getSize();
		var screenW : Number = Fever.client.screen.width;
		var screenH : Number = Fever.client.screen.height;
		
		var scale : Number = Math.max( videoSize.width / screenW, videoSize.height / screenH );
        var newW : Number = videoSize.width / scale;
        var newH : Number = videoSize.height / scale;
        
        _controller.setSize( newW, newH  );
        _controller.move( screenW / 2 - newW / 2, screenH / 2 - newH / 2 );	
	}
}